<?php

namespace App\Admin\User\Controllers;

use App\Admin\User\Models\User;
use App\Classlib\FormCheck;
use Illuminate\Http\Request;
use App\Admin\Controller;

class UserViewController extends Controller
{
    public function __construct(){
        $this->formCheck = new FormCheck();
        $this->user    = new User();
    }

    public function index(Request $request){
        $this->actionLog(Lang('User','view_user_lists'),$request->ip());
        $menuInfo = getMenuFromPath($request->path());
        return view("User.Views.index")
            ->with("thisAction",$menuInfo->url)
            ->with("title",$menuInfo->title);
    }

    public function cors(){
        return view("store.cors")
            ->with("thisAction","/stores")
            ->with("title","编辑门店");
    }

    public function updateUserState(Request $request){
        if(!actionIsView("blacklist_users")){
            return redirect(adminurl("/unauthorized"));
        }
        $checkUserId = $this->formCheck->isEmpty($request->id,"用户ID");
        if(!$checkUserId->code){
            return result("用户ID不能为空！");
        }
        if(!in_array($request->state,array(0,1))){
            return result("更新状态不存在！");
        }
        $result = $this->user->updateUserState($request);
        if($result->code==1){
            if($request->state==1){
                $log_msg = '移出用户黑名单';
            }else{
                $log_msg = '加入用户黑名单';
            }
            $this->actionLog($log_msg);
        }
        return result($result->msg,$result->code);
    }

    //获取用户下级会员所有订单列表
    public function childLists(Request $request){
        $dataContent = $this->user->getChildUserLists($request);
        if (count($dataContent['lists'])) {
            foreach ($dataContent['lists'] as $data) {
                $data->payTimes = $this->user->getPayTimes($data->id);
            }
        } else {
            $dataContent['lists'] = array();
        }
        return adminResult("请求成功",1,$dataContent['count'],$dataContent['lists']);
    }
}